package com.example.siyun.service.impl.loginserviceimpl;

import com.example.siyun.pojo.Userr;
import com.example.siyun.service.loginservice.UserService;
import com.example.siyun.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.acl.Permission;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 全
 * @Date: 2022/10/13 09:01
 * @Description:
 */
@Service("smsUserDetailsService")
public class SmsUserDetailsService implements UserDetailsService {


    @Autowired
    UserService userService;

    @Autowired
    @Lazy
    private PasswordEncoder passwordEncoder;

    /**
     * @param phone 手机号
     */
    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        // 1. 数据库查询手机用户,这里需要写根据手机号查询用户信息，这里写死吧。。。
        System.out.println("-----------");
        System.out.println(phone);
        System.out.println("-----------");
        Userr userInfoDO = userService.phonelogin(phone);
        System.out.println(userInfoDO);
        WebUtils.getSession().setAttribute("usernamename",userInfoDO.getUsername());
        System.out.println(userInfoDO);
        if (userInfoDO == null) {
            throw new UsernameNotFoundException("手机号不存在！");
        }
        // 2. 设置权限集合，后续需要数据库查询
/*        List<GrantedAuthority> authorityList =
                AuthorityUtils.commaSeparatedStringToAuthorityList("role");*/
        // 2. 角色权限集合转为  List<GrantedAuthority>
//        List<Role> roleList = userInfoDO.getRoleList();
//        List<Permission> permissionList = userInfoDO.getPermissionList();
//        List<GrantedAuthority> authorityList = new ArrayList<>();
//        roleList.forEach(e -> {
//            String roleCode = e.getRoleCode();
//            if (StringUtils.isNotBlank(roleCode)) {
//                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleCode);
//                authorityList.add(simpleGrantedAuthority);
//            }
//        });
//        permissionList.forEach(e -> {
//            String code = e.getCode();
//            if (StringUtils.isNotBlank(code)) {
//                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(code);
//                authorityList.add(simpleGrantedAuthority);
//            }
//        });
//        // 3. 返回自定义的用户信息
        List<SimpleGrantedAuthority> authorityList = new ArrayList();
        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_admin");
        authorityList.add(simpleGrantedAuthority);
        return new User(
                userInfoDO.getUsername(),
                passwordEncoder.encode(userInfoDO.getPassword()),authorityList
        );
    }
}
